package cn.stylefeng.guns.modular.app.service;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.modular.app.entity.MemberRechargeAddress;
import cn.stylefeng.guns.modular.app.mapper.MemberRechargeAddressMapper;
import cn.stylefeng.guns.modular.fin.entity.Currency;
import cn.stylefeng.guns.modular.trc.service.TrcRechargeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户充值地址 Service
 */
@Service
public  class MemberRechargeAddressService extends ServiceImpl<MemberRechargeAddressMapper, MemberRechargeAddress>{


    @Autowired
    private TrcRechargeService trcRechargeService;


    public BigDecimal notionalPooling(String coin){
        List<MemberRechargeAddress> balanceAddressList = getBalanceAddressList(coin);
        BigDecimal amount = BigDecimal.ZERO;
        for (MemberRechargeAddress memberRechargeAddress : balanceAddressList) {

            String s = trcRechargeService.transTrc20(memberRechargeAddress.getAddress(),memberRechargeAddress.getRemark(), memberRechargeAddress.getBalance());
            if(!s.equals("ok")){
                continue;
            }
            //设置金额为0
            setAddressBalanceZero(memberRechargeAddress.getId());
            amount = amount.add(memberRechargeAddress.getBalance());
        }


        return amount;
    }



    public Map<String,MemberRechargeAddress> getMemberRechargeAddress(String type){
        LambdaQueryWrapper<MemberRechargeAddress> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberRechargeAddress::getCoin,type);
    //    lqw.select(MemberRechargeAddress::getAddress,MemberRechargeAddress::getMemberId);
        Map<String,MemberRechargeAddress> map = new HashMap<>();
        List<MemberRechargeAddress> list = this.list(lqw);
        for (MemberRechargeAddress memberRechargeAddress : list) {
            map.put(memberRechargeAddress.getAddress(),memberRechargeAddress);
        }
        return map;
    }


    public Boolean activateAddress(Long memberId, String coin, BigDecimal addAmount){

        UpdateWrapper<MemberRechargeAddress> wrapper = Wrappers.update();
        wrapper.setSql(StrUtil.format("balance = balance + {}", addAmount));
        wrapper.setSql(" is_activate = true ");
        wrapper.eq("member_id", memberId);
        wrapper.eq("coin", coin);
          return this.update(wrapper);
    }




    public List<MemberRechargeAddress> getBalanceAddressList(String coin){
        LambdaQueryWrapper<MemberRechargeAddress> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MemberRechargeAddress::getCoin,coin);
     //   lqw.select(MemberRechargeAddress::getAddress,MemberRechargeAddress::getMemberId);
        lqw.gt(MemberRechargeAddress::getBalance,0);
        return list(lqw);
    }


    public Boolean setAddressBalanceZero(Long id){
        LambdaUpdateWrapper<MemberRechargeAddress> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(MemberRechargeAddress::getId,id);
        //   lqw.select(MemberRechargeAddress::getAddress,MemberRechargeAddress::getMemberId);
        lqw.set(MemberRechargeAddress::getBalance,0);
        return update(lqw);
    }

}